﻿<?php 
/*
确认收收货接口,如果是首单就返券
*/

include("conn.php");
mysql_query('SET NAMES UTF8'); 

$result_data_array = array();

$ordersid = $_REQUEST['ordersid'];

if(!$ordersid){
	$result_data_array['code'] = -100;
	$result_data_array['msg'] = '订单信息错误';
	echo json_encode($result_data_array);
	exit;
}

$state=3;
$shou_huo_datetime =  date("Y-m-d H:i:s");//收货时间;
$balance_time = date("Y-m-d H:i:s",(time()+7*3600*24));

//判断该用户在goodsorders表里面有几条订单
$qnum = mysql_query("SELECT count(*) AS num FROM goodsorders WHERE userid = (SELECT userid FROM goodsorders WHERE ordersid = " . $_REQUEST['ordersid'] . ") AND state = 5");
$qnum = mysql_fetch_array($qnum);
if($qnum['num'] == 0) {
	$orders_first = mysql_query("SELECT * FROM orders_first");
	$order_data = mysql_fetch_assoc(mysql_query("SELECT totalprice, userid FROM goodsorders WHERE ordersid = " . $_REQUEST['ordersid']));
	
	while ($row = mysql_fetch_array($orders_first)) {
	$lastTime = date('Y-m-d H:i:s',strtotime('+1 year'));
	$amount = $qnum['totalprice']*($row['first_cate_rate']*0.01);
	$sql = "INSERT INTO orders_users_first (uid, amount, lastAmount, ordersid, firstid, first_cate_use, lastTime ) VALUES (" . $order_data['userid'] .",". $amount .",". $amount . "," . $order_data['userid'] .",".  $row['id']  .",".  $row['first_cate_use'] . ",'" . $lastTime . "')";

	}
	
	$result = mysql_query($sql);
}

$srat1=mysql_query("update goodsorders set state='".$state."',shou_huo_datetime='".$shou_huo_datetime."',balance_time='".$balance_time."' where ordersid='".$ordersid."'");
 
 $rert31=mysql_query("select *from  goodsorders where  ordersid='".$ordersid."' ");
 $rowf1=mysql_fetch_array($rert31);

 $rert32=mysql_query("select *from  shop where  shopid='".$rowf1[shopid]."' ");
 $rowf2=mysql_fetch_array($rert32);

 $rert34=mysql_query("select *from  moneyrun where  anyid='".$ordersid."' and userid='".$rowf2[userid]."' ");
 $rowf4=mysql_fetch_array($rert34);

 $rert36=mysql_query("select *from  moneyrun where  anyid='".$ordersid."' and userid !='".$rowf2[userid]."' ");
 $rowf6=mysql_fetch_array($rert36);

  $states=1;
  $srat1=mysql_query("update moneyrun set yes='".$states."' where anyid='".$ordersid."'");
  
  $srat1=mysql_query("update user set money=money+'".$rowf4[money]."' where userid='".$rowf4[userid]."'");
  
  $srat2=mysql_query("update user set money=money+'".$rowf6[money]."' where userid='".$rowf6[userid]."'");

// 查找快递公司
$query = mysql_query("select * from express_delivery where id=".$rowf1['kuaidi']);
$express_delivery=mysql_fetch_assoc($query);
//查找快递公司 end
$result_data_array['code']   = 200;
$result_data_array['msg']    = '收货成功';
$result_data_array['shopid'] = $rowf1['shopid'];
$result_data_array['danhao'] = $rowf1['danhao'];
$result_data_array['pinyin'] = $express_delivery['pinyin'];
$result_data_array['state']  = $state;
if($result == true) {
	$result_data_array['first_coupon'] = '获取首单返券成功';
}

echo json_encode($result_data_array);
?>